<?php
    use yii\helpers\Html;
    use yii\helpers\Url;
    use yii\widgets\LinkPager;
?>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">

            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>学生列表</h5>
                </div>
                <div class="ibox-content">
                    <div class="form-group form-horizontal form-style">
                        <?=\yii\bootstrap\Tabs::widget([
                            'items'=>[
                                [
                                    'label' => '列表',
                                    'url'=>Url::current(['is_delete'=>0,'page'=>1]),
                                    'active' => !$is_delete
                                ],
                                [
                                    'label' => '回收站',
                                    'url'=>Url::current(['is_delete'=>1,'page'=>1]),
                                    'active' => $is_delete
                                ],
                            ]
                        ]); ?>
                    </div>
                    <div class="form-group form-horizontal form-style">
                        <form class="" action="">
                            <div class="row m-b">
                                <label class="col-sm-1 control-label">注册时间</label>
                                <div class="col-xs-4">
                                    <input type="text" name="input_date" value="<?=$input_date?>" class="form-control" placeholder="选择注册时间范围">
                                </div>
                                <label class="col-sm-1 control-label">关键词</label>
                                <div class="col-sm-2">
                                    <input type="text" name="keyword" value="<?= $keyword?>" class="form-control" placeholder="输入学生ID、名称或手机号">
                                </div>
                                <label class="col-sm-1 control-label">来源</label>
                                <div class="col-sm-2">
                                    <select class="form-control" name="registered_from">
                                        <option value="">全部</option>
                                        <?php if($registered_from_arr) foreach($registered_from_arr as $key => $val){ ?>
                                            <option value="<?=$key?>" <?=$registered_from !== '' && $registered_from == $key ? 'selected':''?> ><?=$val?></option>
                                        <?php }?>
                                    </select>
                                </div>
                            </div>
                            <div class="row m-b">
                                <label class="col-sm-1 control-label">地区</label>
                                <div class="col-sm-4 select_area form-inline">
                                    <?= $area_select?>
                                </div>
                                <label class="col-sm-1 control-label">代理商ID</label>
                                <div class="col-sm-2">
                                    <input type="text" readonly name="agent_id" class="form-control search_agent" value="<?=$agent_id?>">
                                </div>
                                <div class="col-sm-2">
                                    <input type="hidden" name="is_delete" value="<?=$is_delete?>"/>
                                    <button type="submit" class="btn btn-primary">搜索</button>&nbsp;&nbsp;
                                    <?php
                                    $user = \Yii::$app->params['user'];
                                    $user_id = \Yii::$app->user->id;
                                        if (in_array($user_id, $user)) {
                                    ?>
                                            <input type="submit" name="export" class="btn" value="导出">
                                    <?php
                                        }
                                    ?>
                                    <?php if($this->context->isAuth('getui/send-ajax-do')){?>
                                    <input type="button" class="getui btn" value="推送">
                                    <?php }?>
                                </div>
                            </div>
                        </form>
                    </div>
                    <?= \yii\grid\GridView::widget([
                        'dataProvider' => $dataProvider,
                        'rowOptions' => function($model, $key, $index, $grid){
                            return ['data-id' => $model->id];
                        },
                        'tableOptions'=>['class'=>'table table-striped table-bordered table-hover'],
                        'summary'=>'{begin}-{end}，共{totalCount}条数据，共{pageCount}页',
                        'pager' =>['class' => \backend\utils\GoPager::className()],
                        'columns' => [
                            'id',
                            'nickname:text',
                            [
                                'attribute'=>'iconImg',
                                'format'=>'html',
                                'label'=>'头像',
                            ],
                            [
                                'label'=>'电话号码',
                                'attribute'=>'resetTelephone',
                            ],
                            [
                                'label'=>'地区',
                                'value'=>function($v) use ($area){
                                    return implode(' ',$area->getAreaName([$v->province,$v->city,$v->country]));
                                }
                            ],
                            [
                                'attribute'=>'statusName',
                                'label'=>'状态',
                            ],
                            [
                                'attribute'=>'formatDate',
                                'label'=>'注册时间',
                            ],
                            [
                                'label'=>'消费额',
                                'value'=>function($v){
                                    return round($v->ordersAmountTotal,2);
                                }
                            ],
                            [
                                'header'=>'操作',
                                'class' => 'yii\grid\ActionColumn',
                                'template'=>'{detail}　{update}　{delete}',
                                'buttons'=>[
                                    'detail'=>function ($url, $model) {
                                        return $this->context->isAuth('user/detail')?Html::a('详情','javascript:;', ['data-id'=>$model->id,'data-nickname'=>$model->nickname,'class' => 'btn btn-xs btn-info user-detail']):'';
                                    },
                                    'update'=>function ($url, $model) {
                                        return $this->context->isAuth('user/edit')?Html::a('编辑',Url::to(['edit','id'=>$model->id]), ['class' => 'btn btn-xs btn-primary']):'';
                                    },
                                    'delete'=>function($url,$model){
                                        if(!$this->context->isAuth('user/dele-ajax-do')) return '';
                                        if($model->is_delete==0){
                                            return Html::a('删除','#', ['url'=>'dele-ajax-do','data-delete'=>1,'class' => 'btn btn-xs btn-warning ajax_action']);
                                        }else{
                                            return Html::a('恢复','#', ['url'=>'dele-ajax-do','data-delete'=>0,'class' => 'btn btn-xs btn-primary ajax_action']);
                                        }
                                    }
                                ],
                            ],

                        ],
                    ]); ?>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="showDetail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document" style="height: 95%">
            <div class="modal-content" style="height: 100%">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">详情</h4>
                </div>
                <div class="modal-body" style="height: 82%;padding: 0;">
                    <iframe id="detail" src="" style="width: 100%;height: 100%;border: 0;"></iframe>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <!--添加编辑课程-->
    <div class="modal inmodal" id="myModal1" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content animated bounceInRight">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <h4 class="modal-title">推送APP消息</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="ibox float-e-margins">
                                <div class="form-horizontal">
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span class="text-danger">*</span>选择用户：</label>
                                        <div class="col-sm-7 form-inline">
                                            <label><input checked type="radio" name="send_type" value="0" /> 所有人</label><br/>
                                            <label><input type="radio" name="send_type" value="1" /> 指定用户
                                                <input type="text" name="send_user_id" class="form-control" style="height: 25px;display: none;" placeholder="输入用户ID"/></label>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span class="text-danger">*</span>消息类型：</label>
                                        <div class="col-sm-7">
                                            <label><input checked type="radio" name="message_type" value="2" /> 普通消息</label>
                                            <label><input type="radio" name="message_type" value="3" /> 链接网址
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span class="text-danger">*</span>标题：</label>
                                        <div class="col-sm-7">
                                            <input type="text" class="form-control" name="title" value="" placeholder="请输入通知标题" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span class="text-danger">*</span>内容：</label>
                                        <div class="col-sm-7">
                                            <textarea name="body" class="form-control" placeholder="请输入通知内容" ></textarea>
                                        </div>
                                    </div>
                                    <div class="form-group url_box" style="display: none;">
                                        <label class="col-sm-3 control-label"><span class="text-danger">*</span>网址：</label>
                                        <div class="col-sm-7">
                                            <input type="text" class="form-control" name="url" value="" placeholder="请输入网址链接,http(s)://" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary send_message">发送</button>
                </div>
            </div>
        </div>
    </div>
    <script src="/js/plugins/layer/laydate5.0.7/laydate.js" type="text/javascript"></script>
    <script>
        $(function(){
            $('.user-detail').click(function(){
                $('#showDetail').modal('show');
                $('#myModalLabel').text($(this).attr('data-nickname')+'的详情');
                $('#detail').contents().find("body").html("");
                $('#detail').attr('src','/user/detail?id='+$(this).attr('data-id'));
            });
            laydate.render({
                elem: 'input[name="input_date"]'
                ,range: '~'
                ,type: 'datetime'
                ,max: "<?=date('Y-m-d',time()-86400)?>"
                ,btns: ['confirm']
            });
            //推送框
            $('.getui').click(function(){
                $('#myModal1').modal('show');
            });

            $('#myModal1 input[name=send_type]').change(function(){
                if($(this).val() == 1){
                    $('#myModal1 input[name=send_user_id]').show().val('');
                }else{
                    $('#myModal1 input[name=send_user_id]').hide().val('');
                }
            })
            $('#myModal1 input[name=message_type]').change(function(){
                if($(this).val() == 3){
                    $('#myModal1 .url_box').show().find('input[name=url]').val('');
                }else{
                    $('#myModal1 .url_box').hide().find('input[name=url]').val('');
                }
            })

            $('.send_message').click(function(){
                var data={};
                data.send_type = $('#myModal1 input[name=send_type]:checked').val();
                data.message_type = $('#myModal1 input[name=message_type]:checked').val();
                data.send_user_id = $('#myModal1 input[name=send_user_id]').val();
                data.title = $('#myModal1 input[name=title]').val();
                data.body = $('#myModal1 textarea[name=body]').val();
                data.url = $('#myModal1 input[name=url]').val();
                $.post("/getui/send-ajax-do", data,
                function(data){
                    if(data.code == 20000){
                        swal("发送成功！", data.message, "success");
                    }else{
                        swal("发送失败！", data.message, "warning");
                    }
                }, "json");
            })
        });
    </script>